package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysComboboxBo;
import org.dromara.system.domain.vo.SysComboboxVo;
import org.dromara.system.service.ISysComboboxService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 下拉框键值
 *
 * @author Lion Li
 * @date 2024-12-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/combobox")
public class SysComboboxController extends BaseController {

    private final ISysComboboxService sysComboboxService;

    /**
     * 查询下拉框键值列表
     */
    @SaCheckPermission("system:combobox:list")
    @GetMapping("/list")
    public TableDataInfo<SysComboboxVo> list(SysComboboxBo bo, PageQuery pageQuery) {
        return sysComboboxService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/cache")
    public R<Map<Object, Object>> getCombobox(String cacheKey) {
        return R.ok(sysComboboxService.queryCombobox(cacheKey));
    }

    @GetMapping("/cache/children/{cacheName}/{parentKey}")
    public R<Map<Object, Object>> getComboboxChildren(@PathVariable("cacheName") String cacheName, @PathVariable("parentKey") String parentKey) {
        return R.ok(sysComboboxService.queryComboboxChildren(cacheName, parentKey));
    }

    /**
     * 导出下拉框键值列表
     */
    @SaCheckPermission("system:combobox:export")
    @Log(title = "下拉框键值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysComboboxBo bo, HttpServletResponse response) {
        List<SysComboboxVo> list = sysComboboxService.queryList(bo);
        ExcelUtil.exportExcel(list, "下拉框键值", SysComboboxVo.class, response);
    }

    /**
     * 获取下拉框键值详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:combobox:query")
    @GetMapping("/{id}")
    public R<SysComboboxVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysComboboxService.queryById(id));
    }

    /**
     * 新增下拉框键值
     */
    @SaCheckPermission("system:combobox:add")
    @Log(title = "下拉框键值", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysComboboxBo bo) {
        return toAjax(sysComboboxService.insertByBo(bo));
    }

    /**
     * 修改下拉框键值
     */
    @SaCheckPermission("system:combobox:edit")
    @Log(title = "下拉框键值", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysComboboxBo bo) {
        return toAjax(sysComboboxService.updateByBo(bo));
    }

    /**
     * 删除下拉框键值
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:combobox:remove")
    @Log(title = "下拉框键值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysComboboxService.deleteWithValidByIds(List.of(ids), true));
    }
}
